﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using EntityComponentSystem.Framework;
using ECS.Demos.Tetris.Systems;
using ECS.Demos.Tetris.Components;
using EntityComponentSystem.Framework.Math;

namespace ECS.Demos.Tetris.Behavoirs
{
    public class DrawFrameBehavior : Behavior<IRenderSystem>
    {
        public DrawFrameBehavior() : base("rendersystem", "drawframe") { }

        protected override Action GetEntityBehavior(IRenderSystem system, GameEngine ge, int entity)
        {
            var position = ge.GetComponent(entity, "position");
            var size = ge.GetComponent(entity, "size");
            var texture = ge.GetComponent(entity, "texture");
            var color = ge.GetComponent(entity, "color");

            var topleft = ge.GetComponent(entity, "top-left-texture");
            var topright = ge.GetComponent(entity, "top-right-texture");
            var bottomleft = ge.GetComponent(entity, "bottom-left-texture");
            var bottomright = ge.GetComponent(entity, "bottom-right-texture");

            var top = ge.GetComponent(entity, "top-texture");
            var left = ge.GetComponent(entity, "left-texture");
            var right = ge.GetComponent(entity, "right-texture");
            var bottom = ge.GetComponent(entity, "bottom-texture");

            return new Action(() =>
            {
                var w = (int)size["width"] - 1;
                var h = (int)size["height"] - 1;
                var x1 = (int)position["x"];
                var y1 = (int)position["y"];
                var x2 = (int)x1 + w;
                var y2 = (int)y1 + h;
                var c = color.ToColorEx();

                system.DrawTexture(topleft["name"], x1, y1, 1, 1, c);
                system.DrawTexture(topright["name"], x2, y1, 1, 1, c);
                system.DrawTexture(bottomleft["name"], x1, y2, 1, 1, c);
                system.DrawTexture(bottomright["name"], x2, y2, 1, 1, c);

                system.DrawTexture(top["name"], x1 + 1, y1, w - 1, 1, c);
                system.DrawTexture(bottom["name"], x1 + 1, y1 + h, w - 1, 1, c);
                system.DrawTexture(left["name"], x1, y1 + 1, 1, h - 1, c);
                system.DrawTexture(right["name"], x1 + w, y1 + 1, 1, h - 1, c);
            });

        }
    }

}
